package com.casic.ops.entity.dto.pipeline;

import com.alibaba.druid.sql.visitor.functions.Insert;
import com.casic.ops.consts.user.UserHolder;
import com.casic.ops.entity.dto.pipeline.datastructure.graph.Graph;
import com.casic.ops.utils.OpsSnowFlake;
import lombok.Data;
import lombok.experimental.Accessors;
import org.springframework.util.Assert;

import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * 流水线DTO
 * @author cww
 * @date 2022/6/17
 */
@Data
@Accessors(chain = true)
public class PipelineDTO extends CommonDTO {
    /**
     * 应用uuid
     */
    @NotNull(message = "应用uuid不能为空")
    private String applicationUuid;
    /**
     * 流水线名称
     */
    @NotNull(message = "流水线名字不能为空")
    private String pipelineName;
    /**
     * 应用类型
     */
    private String pipelineContext;
    /**
     * 描述
     */
    private String description;
    /**
     * 画布流程图
     */
    private Graph graph;
    /**
     * 触发方式
     */
    private String triggerWay;

    /**
     * 分支
     */
    private String branch;

    public void validate() {
        Assert.notNull(graph, "流程图不能为空");
        Assert.notNull(graph.getNodes(), "节点不能为空");
        Assert.notNull(applicationUuid, "应用ID不能为空");
        Assert.hasLength(pipelineName, "流水线名字不能为空");
        int start = graph.getNodes().stream().filter(nodes -> "START".equals(nodes.getName())).collect(Collectors.toList()).size();
        int end = graph.getNodes().stream().filter(nodes -> "END".equals(nodes.getName())).collect(Collectors.toList()).size();
        Assert.isTrue(start <= 1, "必须包含一个开始节点");
        Assert.isTrue(start >= 1, "最多包含一个开始节点");
        Assert.isTrue(end <= 1, "必须包含一个结束节点");
        Assert.isTrue(end >= 1, "最多包含一个结束节点");
    }

    public void init(){
        this.setCreator(UserHolder.get().getUsername());
        this.setModifier(UserHolder.get().getUsername());
        Date date = new Date();
        this.setGmtCreate(date);
        this.setGmtModified(date);
    }
}
